<!DOCTYPE html>
<!--
  Copyright 2018 Google Inc.

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
-->
<html>
  <head>
    <meta charset="utf-8">
    <title>Menu Surface - Material Components Catalog</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" type="image/png" href="/images/logo_components_color_2x_web_48dp.png">
    <script src="/ready.js"></script>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Mono">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500">
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
    <link rel="stylesheet" href="/assets/menu-surface.css">
  </head>
  <body class="mdc-typography">
    <header class="mdc-toolbar mdc-toolbar--fixed demo-header-toolbar">
      <div class="mdc-toolbar__row">
        <section class="mdc-toolbar__section mdc-toolbar__section--align-start">
          <a href="/" class="catalog-back mdc-toolbar__menu-icon"><i class="material-icons">&#xE5C4;</i></a>
          <span class="mdc-toolbar__title catalog-title">Menu Surface</span>
        </section>
        <section class="mdc-toolbar__section mdc-toolbar__section--align-end mdc-toolbar__section--shrink-to-fit">
          <button id="overflow-icon" class="material-icons mdc-icon-button">more_vert</button>
        </section>
      </div>
    </header>

    <main>
      <div class="mdc-toolbar-fixed-adjust"></div>
      <section class="hero">
        <div class="mdc-menu-surface mdc-menu-surface--open">
          <ul class="mdc-list" role="menu">
            <li class="mdc-list-item" role="menuitem">

              <span class="mdc-list-item__text">
                Back
              </span>
            </li>
            <li class="mdc-list-item" role="menuitem">
              <span class="mdc-list-item__text">
                Forward
              </span>
            </li>
            <li class="mdc-list-item" role="menuitem">
              <span class="mdc-list-item__text">
                Reload
              </span>
            </li>
            <li class="mdc-list-divider" role="separator"></li>
            <li class="mdc-list-item" role="menuitem">
              <span class="mdc-list-item__text">
                Help &amp; Feedback
              </span>
            </li>
            <li class="mdc-list-item" role="menuitem">
              <span class="mdc-list-item__text">
                Settings
              </span>
            </li>
          </ul>
        </div>
      </section>
      <div class="demo-content">
        <div id="demo-wrapper">
          <div class="mdc-menu-surface--anchor">
            <button id="menu-button" class="mdc-button mdc-button--raised demo-button demo-button--normal">
              <span>
                Show<span class="demo-button__normal-text"> Menu Surface</span><span class="demo-button__long-text"> From Here Now!</span>
              </span>
            </button>

            <div class="mdc-menu-surface" tabindex="-1" id="demo-menu">
              <ul class="mdc-image-list standard-image-list demo-menu-surface" role="menu">
                <li role="menuitem" class="mdc-image-list__item" tabindex="0">
                  <div class="mdc-image-list__image-aspect-container">
                    <img class="mdc-image-list__image" src="images/photos/3x2/1.jpg">
                  </div>
                  <div class="mdc-image-list__supporting">
                    <span class="mdc-image-list__label">Text label</span>
                  </div>
                </li>
                <li role="menuitem" class="mdc-image-list__item" tabindex="0">
                  <div class="mdc-image-list__image-aspect-container">
                    <img class="mdc-image-list__image" src="images/photos/3x2/2.jpg">
                  </div>
                  <div class="mdc-image-list__supporting">
                    <span class="mdc-image-list__label">Text label</span>
                  </div>
                </li>
                <li role="menuitem" class="mdc-image-list__item" tabindex="0">
                  <div class="mdc-image-list__image-aspect-container">
                    <img class="mdc-image-list__image" src="images/photos/3x2/3.jpg">
                  </div>
                  <div class="mdc-image-list__supporting">
                    <span class="mdc-image-list__label">Text label</span>
                  </div>
                </li>
                <li role="menuitem" class="mdc-image-list__item" tabindex="0">
                  <div class="mdc-image-list__image-aspect-container">
                    <img class="mdc-image-list__image" src="images/photos/3x2/4.jpg">
                  </div>
                  <div class="mdc-image-list__supporting">
                    <span class="mdc-image-list__label">Text label</span>
                  </div>
                </li>
              </ul>
            </div>
          </div>
        </div>

        <div class="demo-controls-container" dir="ltr">
          <div class="demo-controls">
            <div class="left-column-controls">
              Button Position:
              <div>
                <label><input type="radio" name="position" value="top left" checked> Top left</label>
              </div>
              <div>
                <label><input type="radio" name="position" value="top right"> Top right</label>
              </div>
              <div>
                <label><input type="radio" name="position" value="middle left"> Middle left</label>
              </div>
              <div>
                <label><input type="radio" name="position" value="middle right"> Middle right</label>
              </div>
              <div>
                <label><input type="radio" name="position" value="bottom left"> Bottom left</label>
              </div>
              <div>
                <label><input type="radio" name="position" value="bottom right"> Bottom right</label>
              </div>
            </div>
            <div class="right-column-controls">
              Default Anchor Element Anchor Corner:
              <div>
                <label><input type="radio" name="anchor-position" value="top start" checked> Top start</label>
              </div>

              <div>
                <label><input type="radio" name="anchor-position" value="top end"> Top end</label>
              </div>

              <div>
                <label><input type="radio" name="anchor-position" value="bottom start"> Bottom start</label>
              </div>

              <div>
                <label><input type="radio" name="anchor-position" value="bottom end"> Bottom end</label>
              </div>
            </div>
            <p>
              Anchor Margins:
              <div id="margin-inputs" class="margin-inputs">
                <label>T: <input type="text" id="top-margin" value="0" size="3"></label>
                <label>B: <input type="text" id="bottom-margin" value="0" size="3"></label>
                <label>L: <input type="text" id="left-margin" value="0" size="3"></label>
                <label>R: <input type="text" id="right-margin" value="0" size="3"></label>
              </div>
            </p>
            <div>
              <label><input type="checkbox" name="is-rtl"> RTL</label>
            </div>
            <div>
              <label><input type="checkbox" name="anchor-element"> Change anchor element</label>
            </div>
            <div>
              <label><input type="checkbox" name="hoist-menu"> Hoist Menu Surface to body</label>
            </div>
            <div>
              <label><input type="checkbox" name="animation"> Disable Open Animation</label>
            </div>
            <div>
              <label><input type="checkbox" name="fixed-position"> Fixed Position Menu Surface</label>
            </div>
            <div>
              <label><input type="checkbox" name="right-click"> Enable Right-Click Menu Surface</label>
            </div>
            <hr>
          </div>
        </div>
      </div>


      <div class="mdc-menu-surface" tabindex="-1" id="demo-context-menu">
        <ul class="mdc-image-list standard-image-list demo-menu-surface" role="menu">
          <li role="menuitem" class="mdc-image-list__item" tabindex="0">
            <div class="mdc-image-list__image-aspect-container">
              <img class="mdc-image-list__image" src="images/photos/3x2/1.jpg">
            </div>
            <div class="mdc-image-list__supporting">
              <span class="mdc-image-list__label">Text label</span>
            </div>
          </li>
          <li role="menuitem" class="mdc-image-list__item" tabindex="0">
            <div class="mdc-image-list__image-aspect-container">
              <img class="mdc-image-list__image" src="images/photos/3x2/2.jpg">
            </div>
            <div class="mdc-image-list__supporting">
              <span class="mdc-image-list__label">Text label</span>
            </div>
          </li>
          <li role="menuitem" class="mdc-image-list__item" tabindex="0">
            <div class="mdc-image-list__image-aspect-container">
              <img class="mdc-image-list__image" src="images/photos/3x2/3.jpg">
            </div>
            <div class="mdc-image-list__supporting">
              <span class="mdc-image-list__label">Text label</span>
            </div>
          </li>
          <li role="menuitem" class="mdc-image-list__item" tabindex="0">
            <div class="mdc-image-list__image-aspect-container">
              <img class="mdc-image-list__image" src="images/photos/3x2/4.jpg">
            </div>
            <div class="mdc-image-list__supporting">
              <span class="mdc-image-list__label">Text label</span>
            </div>
          </li>
        </ul>
      </div>
    </main>

    <script src="/assets/material-components-web.js" async></script>
    <script>
      demoReady(function() {
        var menuEl = document.getElementById('demo-menu');
        var menu = mdc.menuSurface.MDCMenuSurface.attachTo(menuEl);
        var menuButtonEl = document.getElementById('menu-button');

        var contextMenuEl = document.getElementById('demo-context-menu');
        var contextMenu = mdc.menuSurface.MDCMenuSurface.attachTo(contextMenuEl);
        contextMenu.hoistMenuToBody();

        menuButtonEl.addEventListener('click', function() {
          menu.open = !menu.open;
        });

        var changeAnchorElement = document.querySelector('input[name="anchor-element"]');
        var hoistMenu = document.querySelector('input[name="hoist-menu"]');
        var fixedPosition = document.querySelector('input[name="fixed-position"]');

        changeAnchorElement.addEventListener('click', function() {
          if (changeAnchorElement.checked) {
            menu.setMenuSurfaceAnchorElement(document.getElementById('overflow-icon'));
            menu.hoistMenuToBody();
            hoistMenu.checked = true;
            hoistMenu.disabled = true;
            fixedPosition.disabled = true;
          } else {
            menu.setMenuSurfaceAnchorElement(menuButtonEl.parentElement);
            hoistMenu.checked = false;
            var event = document.createEvent("Event");
            event.initEvent("change", false, true);
            hoistMenu.dispatchEvent(event);
            hoistMenu.disabled = false;
            fixedPosition.disabled = false;
          }
        });

        hoistMenu.addEventListener('change', function() {
          if (hoistMenu.checked) {
            menu.hoistMenuToBody();
          } else {
            var temp = menuEl.parentElement.removeChild(menuEl);
            menuButtonEl.parentElement.appendChild(temp);
            menu = mdc.menuSurface.MDCMenuSurface.attachTo(menuEl);
            // Trigger events to ensure the new menu is setup to reflect the configurations selected.
            var event = document.createEvent("Event");
            event.initEvent("change", false, true);
            document.querySelector('input[name="anchor-position"]:checked').dispatchEvent(event);
            document.querySelector('input[name="animation"]').dispatchEvent(event);
            document.getElementById('top-margin').dispatchEvent(event);
            document.querySelector('input[name="fixed-position"]').dispatchEvent(event);
          }
        });

        var animation = document.querySelector('input[name="animation"]');
        animation.addEventListener('change', function(evt) {
          menu.quickOpen = evt.target.checked;
        });

        var radios = document.querySelectorAll('input[name="position"]');
        var anchor = document.querySelector('.mdc-menu-surface--anchor');
        // Initialize to top left.
        anchor.style.setProperty('top', '0');
        anchor.style.setProperty('left', '0');
        for (var i = 0; i < radios.length; i++) {
          radios[i].addEventListener('change', function(evt) {
            if (evt.target.checked) {
              if (evt.target.value) {
                anchor.style.removeProperty('top');
                anchor.style.removeProperty('right');
                anchor.style.removeProperty('bottom');
                anchor.style.removeProperty('left');

                var vertical = evt.target.value.split(' ')[0];
                var horizontal = evt.target.value.split(' ')[1];
                if (vertical === 'middle') {
                  anchor.style.setProperty('top', '35%');
                } else {
                  anchor.style.setProperty(vertical, '0');
                }
                anchor.style.setProperty(horizontal, '0');
              }
            }
          });
        }

        radios = document.querySelectorAll('input[name="anchor-position"]');
        for (var i = 0; i < radios.length; i++) {
          radios[i].addEventListener('change', function(evt) {
            if (evt.target.checked) {
              switch (evt.target.value) {
                case "top start":
                  menu.setAnchorCorner(mdc.menuSurface.MDCMenuSurfaceFoundation.Corner.TOP_START);
                  break;
                case "bottom start":
                  menu.setAnchorCorner(mdc.menuSurface.MDCMenuSurfaceFoundation.Corner.BOTTOM_START);
                  break;
                case "top end":
                  menu.setAnchorCorner(mdc.menuSurface.MDCMenuSurfaceFoundation.Corner.TOP_END);
                  break;
                case "bottom end":
                  menu.setAnchorCorner(mdc.menuSurface.MDCMenuSurfaceFoundation.Corner.BOTTOM_END);
                  break;
              }
            }
          });
        }

        var marginInputsEl = document.getElementById('margin-inputs');
        var inputs = marginInputsEl.querySelectorAll('input[type="text"]');
        for (var i = 0; i < inputs.length; i++) {
          inputs[i].addEventListener('change', function(evt) {
            var topMarginInput = document.getElementById('top-margin');
            var bottomMarginInput = document.getElementById('bottom-margin');
            var rightMarginInput = document.getElementById('right-margin');
            var leftMarginInput = document.getElementById('left-margin');
            var margin = {top: parseInt(topMarginInput.value, 10),
                          right: parseInt(rightMarginInput.value, 10),
                          bottom: parseInt(bottomMarginInput.value, 10),
                          left: parseInt(leftMarginInput.value, 10)};
            menu.setAnchorMargin(margin);
          });
        }

        var rtl = document.querySelector('input[name="is-rtl"]');
        rtl.addEventListener('change', function() {
          var demoWrapper = document.body;
          if (rtl.checked) {
            demoWrapper.setAttribute('dir', 'rtl');
          } else {
            demoWrapper.setAttribute('dir', 'ltr');
          }
        });

        fixedPosition.addEventListener('change', function() {
          menu.setFixedPosition(fixedPosition.checked);
          contextMenu.setFixedPosition(fixedPosition.checked);
        });

        var contextMenuFunc = function(clickEvent) {
          contextMenu.setAbsolutePosition(clickEvent.clientX, clickEvent.clientY);
          contextMenu.open = true;
          clickEvent.preventDefault();
          return false;
        };

        var rightClickCheckbox = document.querySelector('input[name="right-click"]');
        rightClickCheckbox.addEventListener('change', function() {
          if (rightClickCheckbox.checked) {
            document.body.addEventListener('contextmenu', contextMenuFunc);
          } else {
            document.body.removeEventListener('contextmenu', contextMenuFunc);
          }
        });

      });
    </script>
  </body>
</html>
